<?php
#### BB2HTML ###################################################################
// Function for converting BBCode to HTML
function bb2html($text) {
	$base =& Base::getInstance();

	if (!$base->config['bbcode_enabled']) return $text;

	$pattern[] = "/\[url=([^<> \n]+?)\](.+?)\[\/url\]/i";
	$replacement[] = '<a href="\\1" target="_blank">\\2</a>';

	$pattern[] = "/\[url\](.+?)\[\/url\]/i";
	$replacement[] = '<a href="\\1" target="_blank">\\1</a>';

	$pattern[] = "/\[quote=([^<> \n]+?)\](.+?)\[\/quote\]/is";
	$replacement[] = '<div class="quote"><b>\\1:</b><br />\\2</div>';

	$pattern[] = "/\[quote\](.+?)\[\/quote\]/is";
	$replacement[] = '<blockquote><b>Quote:</b><br />\\1</blockquote>';

	$pattern[] = "/\[code\](.+?)\[\/code\]/ii";
	$replacement[] = '<code><b>Code:</b><br />\\1</code>';

	$pattern[] = "/\[email=([^<> \n]+?)\](.+?)\[\/email\]/i";
	$replacement[] = '<a href="mailto:\\1">\\2</a>';

	$pattern[] = "/\[email\](.+?)\[\/email\]/is";
	$replacement[] = '<a href="mailto:\\1">\\1</a>';

	$pattern[] = "/\[img](([^<> \n]+?)\.(gif|jpg|jpeg|png))\[\/img\]/i";
	$replacement[] = '<img src="\\1" border="0" alt="\\1" style="vertical-align: middle" />';

	$pattern[] = "/\[img[=]?(left|right)?\](([^<> \n]+?)\.(gif|jpg|jpeg|png))\[\/img\]/is";
	$replacement[] = '<img align="\\1" src="\\2" border="0" alt="\\2" style="padding:5px; vertical-align: middle" />';

	$pattern[] = "/\[b\](.+?)\[\/b\]/is";
	$replacement[] = '<b>\\1</b>';

	$pattern[] = "/\[i\](.+?)\[\/i\]/is";
	$replacement[] = '<i>\\1</i>';

	$pattern[] = "/\[u\](.+?)\[\/u\]/is";
	$replacement[] = '<u>\\1</u>';

	$pattern[] = "/\[s\](.+?)\[\/s\]/is";
	$replacement[] = '<s>\\1</s>';

	$pattern[] = "/\[h1\](.+?)\[\/h1\]/is";
	$replacement[] = '<h1>\\1</h1>';

	$pattern[] = "/\[h2\](.+?)\[\/h2\]/is";
	$replacement[] = '<h2>\\1</h2>';

	$pattern[] = "/\[h3\](.+?)\[\/h3\]/is";
	$replacement[] = '<h3>\\1</h3>';

	$pattern[] = "/\[hr\]/is";
	$replacement[] = '<div class="hr">&nbsp;</div>';

	$pattern[] = "/\[center\](.+?)\[\/center\]/is";
	$replacement[] = '<center>\\1</center>';
	$pattern[] = "/\[align[=]?(left|right|center|justify)?\](.+?)\[\/align\]/is";
	$replacement[] = '<div align="\\1">\\2</div>';

	$pattern[] = "/\[colour=([#a-z0-9]+?)\](.+?)\[\/colour\]/is";
	$replacement[] = '<font color="\\1">\\2</font>';

	$pattern[] = "/\[link=([^<> \n]+?)\](.+?)\[\/link\]/i";
	$replacement[] = '<a href="\\1">\\2</a>';

	$pattern[] = "/\[\*\]/";
	$replacement[] = $base->icon['bb_bullet'];

	$pattern[] = "/\[bp\]/i";
	$replacement[] = $base->icon['bb_bullet'];

	$pattern[] = "/\[arrow]/i";
	$replacement[] = $base->icon['bb_arrow'];

	$pattern[] = "/\[ab=([^<> \n].+?)\](.+?)\[\/ab\]/i";
	$replacement[] = '<abbr title="\\1">\\2</abbr>';

	$pattern[] = "/\[indent\](.+?)\[\/indent\]/is";
	$replacement[] = '<div class="indent">\\1</div>';

	// List
	$pattern[] = "/\[list=(1|a)+\](\\r\\n)?(.+?)\[\/list\](\\r\\n)?/is";
	$replacement[] = '<ul class="style\\1">\\3</ul>';
	$pattern[] = "/\[list=(1|a)+\](.+?)\[\/list\](\\r\\n)?/is";
	$replacement[] = '<ul class="style\\1">\\2</ul>';
	$pattern[] = "/\[list\](\\r\\n)?(.+?)\[\/list\](\\r\\n)?/is";
	$replacement[] = '<ul>\\2</ul>';
	$pattern[] = "/\[list\](.+?)\[\/list\](\\r\\n)?/is";
	$replacement[] = '<ul>\\1</ul>';
	$pattern[] = "/\[\*\](.+?)\[\/\*\](\\r\\n)?/is";
	$replacement[] = '<li>\\1</li>';

	// HTML
	$br_array = array('<br>', '<br />');
	$endpos = NULL;
	if (strstr($text, '[html]')) $text = ' '.$text;
	while($startpos = strpos($text, '[html]', $endpos)) {
		if ($endpos = strpos($text, '[/html]', $endpos + 7)) {
			$text = substr($text, 0, $startpos)
			.str_replace($br_array, '', nl2br(substr($text, $startpos, $endpos - $startpos)))
			.substr($text, $endpos);
		}
	}
	$pattern[] = "/\[html\](.+?)\[\/html\]/is";
	$replacement[] = '\\1';

	$text = preg_replace($pattern, $replacement, $text);
	return $text;
}
?>
